package yunjiao.designpatterns.combinator;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.function.Function;

/**
 * {@link Around} 示例
 *
 * @author yangyunjiao
 */
@Slf4j
public class AroundExample {
    void demo() {
        log.info("----------------------------------");
        log.info("Starting AROUND combinator demo...");
        log.info("----------------------------------");

        Function<BigDecimal, String> addTaxDecorated =
                Around.decorate(this::addTax, this::around);

        String result = addTaxDecorated.apply(new BigDecimal("10000"));

        log.info("Done - Result is {}", result);
    }

    private String addTax(BigDecimal amount) {
        log.info("Adding heavy taxes to poor citizen...");
        return "$" + amount.multiply(new BigDecimal("1.22"));
    }

    private void around(Around.Executable<String> function, BigDecimal argument) {
        log.info("BEFORE: Amount is {}", argument);
        String result = function.execute(); // function executed here!
        log.info("AFTER: Result is {}", result);
    }
}
